package com.woniu108.valueAdded.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.woniu108.carService.commons.log.AdminHistoryLog;
import com.woniu108.carService.commos.dto.VipLevelFormDto;
import com.woniu108.carService.commos.exception.WoniuException;
import com.woniu108.carService.commos.model.Result;
import com.woniu108.carService.commos.util.JWTUtil;
import com.woniu108.carService.commos.util.RedisUtil;
import com.woniu108.valueAdded.form.AddVipLevelForm;
import com.woniu108.valueAdded.model.VipLevelForm;
import com.woniu108.valueAdded.param.AddVipLevelParam;
import com.woniu108.valueAdded.service.VipLevelFormService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * vip等级表 前端控制器
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@RestController
@RequestMapping("/vipLevelForm")
@Api(tags = "会员等级信息接口")
public class VipLevelFormController {

    @Resource
    private RedisUtil redisUtil;
    @Resource
    private VipLevelFormService vipLevelFormService;

    public String getUserId(String token){
        String refreshToken=redisUtil.getString(token);
        String userId= JWTUtil.parseUserToken(refreshToken).get("userId").toString();
        return userId;
    }

    /**
     *获取规定vip等级信息
     * @return List<VipLevelFormDto> vip分级依据信息
     */
    @GetMapping("/getVipLevelFormList.c")
    @ApiOperation("获取规定vip等级信息")
    public Result<List<VipLevelFormDto>> getVipLevelFormList(){
        List<VipLevelForm> list = vipLevelFormService.list();
        List<VipLevelFormDto> dtos=new ArrayList<>();
        for (VipLevelForm vipLevelForm : list) {
            VipLevelFormDto dto = BeanUtil.toBean(vipLevelForm, VipLevelFormDto.class);
            dtos.add(dto);
        }
        return Result.newListInstance(VipLevelFormDto.class).setData(dtos);
    }

    /**
     * 更改vip等级信息
     * @param form 通用vip等级信息
     * @return result 是否正常访问接口结果
     */
    @PostMapping("/changeVipLeveForm.api")
    @ApiOperation("更改vip等级信息")
    @AdminHistoryLog
    public Result changeVipLeveForm(@RequestBody VipLevelFormDto form){
        VipLevelForm vipLevelForm = vipLevelFormService.getById(form.getVipLevelId());
        if(ObjectUtil.isEmpty(vipLevelForm)) throw new WoniuException("vip等级编号不存在",10001);
        VipLevelForm levelForm = BeanUtil.toBean(form, VipLevelForm.class);
        //参数为空时service的updateById方法不会将null更新到数据库
        vipLevelFormService.updateById(vipLevelForm);
        return Result.getResult().setMsg("更改vip等级信息成功");
    }

    /**
     * 新增vip等级档位
     * @param form
     * @return result 是否正常访问接口结果
     */
    @PostMapping("/addVipLevelForm.api")
    @AdminHistoryLog
    public Result addVipLevelForm(@RequestBody AddVipLevelForm form){
        AddVipLevelParam param=BeanUtil.toBean(form,AddVipLevelParam.class);
        List<VipLevelFormDto> formDtos = this.getVipLevelFormList().getData();
        for (VipLevelFormDto formDto : formDtos) {
            //检测是否存在数据重复情况
            if(form.getVipLevelName().equals(param.getVipLevelName())) throw new WoniuException("会员等级名称已存在",10001);
            if(form.getVipLevelScore().compareTo(param.getVipLevelScore())==0) throw new WoniuException("积分档位已存在",10001);
        }
        //添加到数据库
        VipLevelForm vipLevelForm = BeanUtil.toBean(param, VipLevelForm.class);
        vipLevelFormService.save(vipLevelForm);
        //调用更新数据,准备重排
        List<VipLevelForm> forms = vipLevelFormService.list();
        //若不存在,则新增并将vip等级根据规定的积分门槛重新升序排列
        List<VipLevelForm> formList = forms.stream().sorted((o1, o2) -> o1.getVipLevelScore().compareTo(o2.getVipLevelScore())>0?1:-1).collect(Collectors.toList());
        int i=0;
        for (VipLevelForm levelForm : formList) {
            //按升序重新分配VipLevel并更新数据,初始等级为1
            i=i+1;
            levelForm.setVipLevel(i);
            vipLevelFormService.updateById(levelForm);
        }
        return Result.getResult().setMsg("新增vip等级档位成功");
    }

    /**
     * 删除vip等级信息
     * @param vipLevelId vip等级信息编号
     * @return result 是否正常访问接口结果
     */
    @GetMapping("/deleteVipLevelForm.api")
    @ApiOperation("删除vip等级信息")
    @ApiImplicitParam(name = "vipLevelId" ,value = "vip等级信息编号")
    @AdminHistoryLog
    public Result deleteVipLevelForm(Integer vipLevelId){
        VipLevelForm vipLevelForm = vipLevelFormService.getById(vipLevelId);
        if(ObjectUtil.isEmpty(vipLevelForm)) throw new WoniuException("vip等级信息不存在",10001);
        vipLevelFormService.removeById(vipLevelForm);
        return Result.getResult().setMsg("删除vip等级信息成功");
    }

}

